//@author A0096759E

package googlecal;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import applogger.AppLogger;

public class GoogleAPIInternalStorage {
	
	private static final String GOOGLE_CAL_STORAGE_FILENAME = "GoogleCalIDStorage.txt";
	private static final String GOOGLE_TASK_STORAGE_FILENAME = "GoogleTaskIDStorage.txt";
	private static final String ERROR_MSG_READ_SCHEDULE_FAILED = "ERROR! File couldn't be read. Either file does not exist, or corrupted!";
	private static final String ERROR_MSG_SAVE_SCHEDULE_FAILED = "ERROR! Unable to save latest changes! Please Restart and try again!";
	private static final String SUCCESS_MSG_SAVE_GCAL_ID = "Google Calendar ID Saved!";
	private static final String SUCCESS_MSG_SAVE_GTASK_ID = "Google Task ID Saved!";

	/**
	 * Saves the input Google Calendar ID to a text file "GoogleCalIDStorage.txt"
	 * @param toSave - the GCal ID to save
	 */
	public static void saveGoogleCalID(String toSave) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(GOOGLE_CAL_STORAGE_FILENAME));
            out.write(toSave);
            out.close();
            AppLogger.log(SUCCESS_MSG_SAVE_GCAL_ID);
        } catch (IOException e) {
        	AppLogger.log(ERROR_MSG_SAVE_SCHEDULE_FAILED);
        }
	}
	
	/**
	 * Returns the previously saved Google Calendar ID from text file
	 * @return loadedStringFromFile - the GCal ID previously saved
	 */
	public static String readPreviousGoogleCalID() {
		try {
			FileReader inputFile = new FileReader(GOOGLE_CAL_STORAGE_FILENAME);
		    BufferedReader br = new BufferedReader(inputFile);
		    
		    String loadedStringFromFile = br.readLine();
		    inputFile.close();
		    return loadedStringFromFile;
		} catch (IOException noSuchFile) {
			return ERROR_MSG_READ_SCHEDULE_FAILED;
		}
	}	
	
	/**
	 * Saves the input Google Task ID to a text file "GoogleTaskIDStorage.txt"
	 * @param toSave - the GTask ID to save
	 */
	public static void saveGoogleTaskID(String toSave) {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(GOOGLE_TASK_STORAGE_FILENAME));
            out.write(toSave);
            out.close();
            AppLogger.log(SUCCESS_MSG_SAVE_GTASK_ID);
        } catch (IOException e) {
        	AppLogger.log(ERROR_MSG_SAVE_SCHEDULE_FAILED);
        }
	}
	
	/**
	 * Returns the previously saved Google Task ID from text file
	 * @return loadedStringFromFile - the GTask ID previously saved
	 */
	public static String readPreviousGoogleTaskID() {
		try {
			FileReader inputFile = new FileReader(GOOGLE_TASK_STORAGE_FILENAME);
		    BufferedReader br = new BufferedReader(inputFile);
		    
		    String loadedStringFromFile = br.readLine();
		    inputFile.close();
		    return loadedStringFromFile;
		} catch (IOException noSuchFile) {
			return ERROR_MSG_READ_SCHEDULE_FAILED;
		}
	}	
}
